const request = require('./request.js').default;

/**
 * 目标类型常量
 */
const TARGET_TYPES = {
  QUOTE: 1,    // 书摘
  NOVEL: 2,    // 小说
  CHAPTER: 3   // 章节
};

/**
 * 通用互动功能 API
 * 支持书摘、小说、章节等多种内容类型的点赞、收藏、评论功能
 */
const interactionAPI = {
  /**
   * 目标类型常量
   */
  TARGET_TYPES,

  /**
   * 点赞
   * @param {number} targetType - 目标类型（1-书摘，2-小说，3-章节）
   * @param {string|number} targetId - 目标ID
   */
  like: (targetType, targetId) => {
    return request(`/interaction/like?targetType=${targetType}&targetId=${targetId}`, 'POST');
  },

  /**
   * 取消点赞
   * @param {number} targetType - 目标类型（1-书摘，2-小说，3-章节）
   * @param {string|number} targetId - 目标ID
   */
  unlike: (targetType, targetId) => {
    return request(`/interaction/unlike?targetType=${targetType}&targetId=${targetId}`, 'POST');
  },

  /**
   * 收藏
   * @param {number} targetType - 目标类型（1-书摘，2-小说，3-章节）
   * @param {string|number} targetId - 目标ID
   */
  favorite: (targetType, targetId) => {
    return request(`/interaction/favorite?targetType=${targetType}&targetId=${targetId}`, 'POST');
  },

  /**
   * 取消收藏
   * @param {number} targetType - 目标类型（1-书摘，2-小说，3-章节）
   * @param {string|number} targetId - 目标ID
   */
  unfavorite: (targetType, targetId) => {
    return request(`/interaction/unfavorite?targetType=${targetType}&targetId=${targetId}`, 'POST');
  },

  /**
   * 获取评论列表
   * @param {number} targetType - 目标类型（1-书摘，2-小说，3-章节）
   * @param {string|number} targetId - 目标ID
   * @param {Object} params - 分页参数
   * @param {number} params.page - 页码
   * @param {number} params.pageSize - 每页数量
   */
  getComments: (targetType, targetId, params = {}) => {
    const { page = 1, pageSize = 20 } = params;
    return request(`/interaction/comments?targetType=${targetType}&targetId=${targetId}&page=${page}&pageSize=${pageSize}`, 'GET');
  },

  /**
   * 添加评论
   * @param {number} targetType - 目标类型（1-书摘，2-小说，3-章节）
   * @param {string|number} targetId - 目标ID
   * @param {Object} data - 评论数据
   * @param {string} data.content - 评论内容
   * @param {string|number} data.parentId - 父评论ID（可选）
   */
  addComment: (targetType, targetId, data) => {
    return request(`/interaction/comment?targetType=${targetType}&targetId=${targetId}`, 'POST', data);
  },

  /**
   * 回复评论
   * @param {string|number} commentId - 评论ID
   * @param {Object} data - 回复数据
   * @param {string} data.content - 回复内容
   */
  replyComment: (commentId, data) => {
    return request(`/interaction/reply/${commentId}`, 'POST', data);
  },

  // ========== 便捷方法（针对特定内容类型） ==========

  /**
   * 书摘相关互动
   */
  quote: {
    like: (quoteId) => interactionAPI.like(TARGET_TYPES.QUOTE, quoteId),
    unlike: (quoteId) => interactionAPI.unlike(TARGET_TYPES.QUOTE, quoteId),
    favorite: (quoteId) => interactionAPI.favorite(TARGET_TYPES.QUOTE, quoteId),
    unfavorite: (quoteId) => interactionAPI.unfavorite(TARGET_TYPES.QUOTE, quoteId),
    getComments: (quoteId, params) => interactionAPI.getComments(TARGET_TYPES.QUOTE, quoteId, params),
    addComment: (quoteId, data) => interactionAPI.addComment(TARGET_TYPES.QUOTE, quoteId, data)
  },

  /**
   * 小说相关互动
   */
  novel: {
    like: (novelId) => interactionAPI.like(TARGET_TYPES.NOVEL, novelId),
    unlike: (novelId) => interactionAPI.unlike(TARGET_TYPES.NOVEL, novelId),
    favorite: (novelId) => interactionAPI.favorite(TARGET_TYPES.NOVEL, novelId),
    unfavorite: (novelId) => interactionAPI.unfavorite(TARGET_TYPES.NOVEL, novelId),
    getComments: (novelId, params) => interactionAPI.getComments(TARGET_TYPES.NOVEL, novelId, params),
    addComment: (novelId, data) => interactionAPI.addComment(TARGET_TYPES.NOVEL, novelId, data)
  },

  /**
   * 章节相关互动
   */
  chapter: {
    like: (chapterId) => interactionAPI.like(TARGET_TYPES.CHAPTER, chapterId),
    unlike: (chapterId) => interactionAPI.unlike(TARGET_TYPES.CHAPTER, chapterId),
    getComments: (chapterId, params) => interactionAPI.getComments(TARGET_TYPES.CHAPTER, chapterId, params),
    addComment: (chapterId, data) => interactionAPI.addComment(TARGET_TYPES.CHAPTER, chapterId, data)
  },

      /**
       * 获取用户收藏列表
       * @param {number} targetType - 目标类型（可选，1-书摘，2-小说，3-章节）
       * @param {number} page - 页码
       * @param {number} pageSize - 每页数量
       */
      getUserFavorites: (targetType = 1, page = 1, pageSize = 20) => {
        let url = `/interaction/favorites?page=${page}&pageSize=${pageSize}`;
        if (targetType) {
          url += `&targetType=${targetType}`;
        }
        return request(url, 'GET');
      }
};

module.exports = {
  interactionAPI,
  TARGET_TYPES
};
